﻿using AdminBase.Model;
using System.Text;

namespace AdminBase.Common.Helpers
{
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public class FilesHelper
    {
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="directoryPath">路径</param>
        public static void CreateDir(string directoryPath)
        {
            if (directoryPath.Length == 0) return;
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }

        /// <summary>
        /// 判断文件夹是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }

        /// <summary>
        /// 获取文件夹下的所有文件
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <returns></returns>
        /// <exception cref="DirectoryNotFoundException"></exception>
        public static FileInfo[] GetFiles(string directoryPath)
        {
            if (!IsExistDirectory(directoryPath))
            {
                throw new DirectoryNotFoundException();
            }
            var root = new DirectoryInfo(directoryPath);
            return root.GetFiles();
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="Path">路径</param>
        /// <returns></returns>
        public static string ReadFile(string Path)
        {
            string s;
            if (!File.Exists(Path))
                s = "不存在相应的目录";
            else
            {
                var f2 = new StreamReader(Path, Encoding.Default);
                s = f2.ReadToEnd();
                f2.Close();
                f2.Dispose();
            }
            return s;
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="OriginalFile">原路径</param>
        /// <param name="NewFile">新路径</param>
        public static void MoveFile(string OriginalFile, string NewFile)
        {
            File.Move(OriginalFile, NewFile);
        }

        /// <summary>
        /// 是否匹配格式
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool IsMatchFormat(string fileName, string type = "")
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return false;
            }
            // 获取文件扩展名（不带点）
            string fileExtension = Path.GetExtension(fileName)?.TrimStart('.')?.ToUpper();
            switch (type)
            {
                case "image":
                    // 将枚举转换为数组
                    ImageTypeEnum[] types = Enum.GetValues(typeof(ImageTypeEnum)).Cast<ImageTypeEnum>().ToArray();
                    // 检查扩展名是否在枚举值中
                    return Array.Exists(types, e => Enum.GetName(typeof(ImageTypeEnum), e).ToUpper() == fileExtension);
                default:
                    // 将枚举转换为数组
                    FileTypeEnum[] fileTypes = Enum.GetValues(typeof(FileTypeEnum)).Cast<FileTypeEnum>().ToArray();
                    // 检查扩展名是否在枚举值中
                    return Array.Exists(fileTypes, e => Enum.GetName(typeof(FileTypeEnum), e).ToUpper() == fileExtension);
            }
        }
    }
}
